<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','title','image','status');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmslive_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),
			
			array('op'=>'edit','title'=>'添加直播'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'封面','type'=>'image'),								
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		),
		'search'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}


if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_cmslive_list',array('id'=>$id));
	$item['menu']=json_decode($item['menu'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_cmslive_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['status']=array('0'=>'关闭','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'post'=>'post',
		'desc'=>'直播设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'直播管理'),
			
			array('op'=>'edit','title'=>'添加直播')
		),
		'edits'=>array(
		array(
		'title'=>'直播设置',
		'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'image','title'=>'封面','type'=>'image'),
			array('name'=>'time','title'=>'直播时间','type'=>'time'),

			array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'group'),
			array('name'=>'type','title'=>'直播平台类型','type'=>'radio','options'=>'type'),
			array('name'=>'pt','title'=>'直播平台','type'=>'radio','options'=>'sxt','show'=>array('type'=>1)),
			array('name'=>'pt','title'=>'直播平台','type'=>'radio','options'=>'dsf','show'=>array('type'=>2)),
			array('name'=>'pt','title'=>'直播平台','type'=>'radio','options'=>'fa','show'=>array('type'=>3)),
			array('name'=>'url','title'=>'url地址','type'=>'string','show'=>array('type'=>2)),
			array('name'=>'url','title'=>'url地址','type'=>'string','show'=>array('type'=>1)),
			array('name'=>'shipin','title'=>'视频流','type'=>'string','show'=>array('type'=>3),'help'=>'视频流地址为后缀名为http://域名/xxxxx.m3u8链接'),
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'ishome','title'=>'是否推荐','type'=>'radio','options'=>'is'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'radio','options'=>'is'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			),
			),
		array(
		'title'=>'菜单设置',
		'list'=>array(
			array('type'=>'array','name'=>'menu','add_tip'=>'添加一个菜单',
				'list'=>array(
					array('type'=>'string','name'=>'title','title'=>'菜单'),	
					
				),
				'details'=>array(
					array('type'=>'string','name'=>'title','title'=>'菜单'),	
					array('type'=>'string','name'=>'content','title'=>'内容'),
				)
				),
			),
			),
		array(
		'title'=>'分享/公告设置',
		'list'=>array(
			array('name'=>'notice','title'=>'公告内容','type'=>'string'),
			array('name'=>'share_title','title'=>'分享标题','type'=>'string'),
			
			array('name'=>'share_image','title'=>'分享图片','type'=>'string'),
			array('name'=>'share_desc','title'=>'分享描述','type'=>'string'),
			),
		),
	),
	);
	$return['type']=array('1'=>'第三方平台','2'=>'智能摄像头','3'=>'行业解决方案');
	$return['sxt']=array('3'=>'斗鱼直播','4'=>'熊猫直播','5'=>'花椒直播','6'=>'映客直播');		
	$return['dsf']=array('1'=>'青果直播','2'=>'萤石直播');
	$return['fa']=array('7'=>'腾讯云直播','8'=>'阿里云直播','9'=>'其它');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('image','title','type','time','pt','notice','status','share_image','share_desc','share_title','menu','group_id','url','ishome','ishot','paixu','shipin'),$postdata);
	$data['menu']=json_encode($data['menu']);
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_cmslive_list',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$sign=pdo_getcolumn('jy_cmslive_sign',array('name'=>'admin'),'sign');
				$item=$this->plugin_setting('setting');
				if(empty($sign)){
						
						$identifier='admin';
						$private_key_path=ROOT_D .'/addons/jy_cmslive/private_key';
						$sign=$this->signature($identifier, $item['appid'], $private_key_path);
						$sign=$sign['0'];
						
						if($sign){
							pdo_insert('jy_cmslive_sign',array('uniacid'=>$_SESSION['uniacid'],'name'=>$identifier,'createtime'=>TIMESTAMP,'sign'=>$sign));
						}else{
							exi('生成用户签名失败，请检查云服务设置是否正确','error','/setting/');
						}
						
				}
				$url='https://console.tim.qq.com/v4/group_open_http_svc/create_group?usersig='.$sign.'&identifier=admin&sdkappid='.$item['appid'].'&random='.random(32,true).'&contenttype=json';
				$name=$data['title'].random(4);
				$post=array('Type'=>'AVChatRoom','Name'=>$name);
				$re=cfc('http')->ihttp_post($url,json_encode($post));
				$re['content']=json_decode($re['content'],true);
				if($re['content']['ActionStatus']=='OK'){
						$data['chatroomid']=$re['content']['GroupId'];
				}else{
					exi('创建群聊失败，请检查云服务设置是否正确'.$re['content']['ErrorCode'],'error','/setting/');
				}
				
				//$get = json_decode(,true);
				
				pdo_insert('jy_cmslive_list',$data);
				exi('添加成功！','','list');
				}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cmslive_list',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cmslive_list',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>